package managesystem.login;

import managesystem.JDBCUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.Connection;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: Register
 * @description: 注册界面
 * @date 2021/12/2 17:00
 */
public class Register {
    JFrame frame = new JFrame();

    JPanel titleP = new JPanel();
    JLabel title = new JLabel("注册");

    JPanel p1 = new JPanel();
    JLabel user = new JLabel("账号：");
    JTextField userText = new JTextField(20);

    JPanel p2 = new JPanel();
    JLabel pwd = new JLabel("密码：");
    JTextField pwdText = new JTextField(20);

    JPanel p3 = new JPanel();
    JLabel privateKey = new JLabel("秘钥：");
    JTextField privateKeyText = new JTextField(20);

    JPanel p4 = new JPanel();
    JLabel level = new JLabel("权限：");
    JComboBox levelBox = new JComboBox();

    JPanel hintP = new JPanel();
    JLabel hint = new JLabel();

    JPanel buttonP = new JPanel();
    JButton registerButton = new JButton("注册");
    JButton returnButton = new JButton("返回");

    JLabel hide1 = new JLabel();
    JLabel hide2 = new JLabel();
    JLabel hide3 = new JLabel();

    public void init() {
        title.setFont(new Font("楷体", Font.BOLD, 36));
        user.setFont(new Font("微软雅黑", Font.BOLD, 18));
        pwd.setFont(new Font("微软雅黑", Font.BOLD, 18));
        privateKey.setFont(new Font("微软雅黑", Font.BOLD, 18));
        level.setFont(new Font("微软雅黑", Font.BOLD, 18));

        hint.setFont(new Font("微软雅黑", Font.BOLD, 16));
        hint.setForeground(Color.RED);

        registerButton.setFont(new Font("微软雅黑", Font.BOLD, 16));
        returnButton.setFont(new Font("微软雅黑", Font.BOLD, 16));

        levelBox.addItem("请选择你的身份");
        levelBox.addItem("扶贫人员");
        levelBox.addItem("贫困人员");

        titleP.add(title);

        p1.add(user);
        p1.add(userText);

        p2.add(pwd);
        p2.add(pwdText);

        p3.add(privateKey);
        p3.add(privateKeyText);

        p4.add(level);
        p4.add(levelBox);

        hintP.add(hint);

        buttonP.add(registerButton);
        buttonP.add(hide1);
        buttonP.add(hide2);
        buttonP.add(hide3);
        buttonP.add(returnButton);


        frame.setLayout(new GridLayout(7, 1));
        frame.add(titleP);
        frame.add(p1);
        frame.add(p2);
        frame.add(p3);
        frame.add(p4);
        frame.add(hintP);
        frame.add(buttonP);

        frame.setVisible(true);
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        userText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                userText.setText("");
            }
        });

        pwdText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                pwdText.setText("");
            }
        });

        privateKeyText.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                privateKeyText.setText("");
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Connection connection = JDBCUtils.getConnection();

                String sql = "select * from user_account";
                ArrayList<UserAccount> accounts = JDBCUtils.verificationUserAccount(connection, sql);

                int equalsCount = 0;
                int index = levelBox.getSelectedIndex();
                for (int i = 0; i < accounts.size(); i++) {
                    if (userText.getText().equals(accounts.get(i).getAccount())) {
                        equalsCount++;
                        hint.setText("该账号已经存在，请重新输入！");
                    }
                }

                if (equalsCount == 0) {
                    //贫困人员
                    if (index == 2) {
                        String account = userText.getText();
                        String pwd = pwdText.getText();

                        String insert = "insert into user_account(account, password, level) values(?, ?, ?)";
                        JDBCUtils.addUser(connection, insert, account, pwd, 3);

                        hint.setText("贫困人员账号创建成功，请返回登录界面！");
                    }

                    //扶贫人员
                    if (index == 1) {
                        String account = userText.getText();
                        String pwd = pwdText.getText();
                        String key = privateKeyText.getText();

                        if (key.equals("439677")) {
                            String insert = "insert into user_account(account, password, level) values(?, ?, ?)";
                            JDBCUtils.addUser(connection, insert, account, pwd, 2);

                            hint.setText("扶贫人员账号创建成功，请返回登录界面！");
                        } else {
                            hint.setText("创建扶贫人员账号请向管理员获得正确的秘钥");
                        }
                    }
                }

                JDBCUtils.closeResources(connection, null);
            }
        });

        returnButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Login().init();
            }
        });
    }
}
